class PcapHandler
   def initialize(dev, filter=nil)
       @dev = dev 
       @filter = filter 
   end

   def get_default_bars
       ["ip_src", "sport", "dport", "ip_dst"]
   end

   def run()
       cap = Pcap::Capture.open_live(@dev)

       if @filter
           puts "[PcapHandler] using filter: #{@filter}" 
           cap.setfilter(@filter)
       end

       cap.loop do |pkt|
           case pkt.class.to_s
           when "Pcap::TCPPacket"
               conn = TCPConnection.new(Time.now, pkt) 
           when "Pcap::UDPPacket"
               conn = UDPConnection.new(Time.now, pkt)
           when "Pcap::ICMPEcho"
               conn = ICMPConnection.new(Time.now, pkt) 
           when "Pcap::ICMPEchoReply" 
               conn = ICMPConnection.new(Time.now, pkt) 
           when "Pcap::ICMPUnreach"
               conn = ICMPConnection.new(Time.now, pkt)
           when "Pcap::ICMPTimxceed"
               conn = ICMPConnection.new(Time.now, pkt)
           else
#               pp pkt
           end

           if conn
               $connections.add_or_update(conn)
           end
       end
   end
end
